package com.compass.doctor.preview

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.compass.doctor.databinding.ActivityPreviewBinding
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.constant.KEY_LIST
import com.compass.framework.constant.KEY_POSITION
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.utils.DensityUtil

/**
 * Description： 预览图片
 * Date:2023/12/11 15:38
 */
class PreviewPicActivity : BaseVBActivity<ActivityPreviewBinding>() {

    private lateinit var viewPageAdapter: CustomPreviewAdapter
    var curPosition = 0
    var mData = ArrayList<LocalMedia>()

    private var totalNum: Int = 0//总数


    companion object {
        fun start(context: Context, position: Int = 0, data: ArrayList<LocalMedia>) {
            val intent = Intent(context, PreviewPicActivity::class.java)
            intent.putExtra(KEY_POSITION, position)
            intent.putExtra(KEY_LIST, data)
            context.startActivity(intent)
        }
    }


    override fun initView(savedInstanceState: Bundle?) {
        curPosition = intent.getIntExtra(KEY_POSITION, 0)
        mData = intent.parcelableArrayList(KEY_LIST)!!
        totalNum = mData.size
        initViewPagerData(mData)
    }


    private fun initViewPagerData(data: ArrayList<LocalMedia>) {
        viewPageAdapter = CustomPreviewAdapter()
        viewPageAdapter.setData(data)

        mBinding.viewpager2.orientation = ViewPager2.ORIENTATION_HORIZONTAL
        mBinding.viewpager2.adapter = viewPageAdapter

        mBinding.viewpager2.registerOnPageChangeCallback(pageChangeCallback)
        mBinding.viewpager2.setPageTransformer(
            MarginPageTransformer(
                DensityUtil.dip2px(this@PreviewPicActivity, 3f)
            )
        )
        mBinding.viewpager2.setCurrentItem(curPosition, false)

    }


    private val pageChangeCallback: OnPageChangeCallback = object : OnPageChangeCallback() {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
        }

        override fun onPageSelected(position: Int) {
            curPosition = position
            mBinding.tvTitleBar.setMiddleText((curPosition + 1).toString() + "/" + totalNum)

        }
    }


 


}